'use strict';
const dayjs = require('dayjs');
module.exports = app => {
    const { INTEGER, STRING, TEXT } = app.Sequelize;
    const Question = app.model.define(
        'Question',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            course_id: INTEGER,
            category_id: INTEGER,
            name: TEXT,
            type: STRING,
            delete_flag: INTEGER,
            status: INTEGER,
            score: INTEGER,
            created_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('created_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'question',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'created_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    Question.associate = function() {
        app.model.Question.hasMany(app.model.QuestionOption, { foreignKey: 'question_id', targetKey: 'id' });
        // 分类
        app.model.Question.belongsTo(app.model.CourseCategories, { foreignKey: 'category_id', targetKey: 'id' });
        // 课程
        app.model.Question.belongsTo(app.model.Course, { foreignKey: 'course_id', targetKey: 'id' });
    }
    return Question;
};
